<?php
$device_obj = new device();
$project_obj = new project();

$action_ary = array(
	'getini' => 'gas_act_getinino',  //获取参数
	'record'=>'gas_act_record',  //加油
	'editinfo'=>'gas_act_editinfo',//修改记录信息
	'improt_out'=>'gas_act_improt_out', //导出加油明细
	'improt_out_report'=>'gas_act_improt_report',  //导出加油统计报表
	'improt_in'=>'gas_act_improt_in', //导出进油明细
    'improt_in_report'=>'gas_act_improt_in_report',  //导出进油统计报表
    'nouse'=>'gas_act_nouse', //无效记录
    'reback'=>'gas_act_reback', //还原数据
    'delop'=>'gas_act_delop'  //彻底删除数据
);

//action
//*+------------------------------------------------------------------------+*/


/**
 * 获取油罐号、设备号
 */
function gas_act_getinino(){
	global $device_obj, $project_obj;

	$rtn = array('state'=>false);
	//得到设备信息
	$no = request('no');    //设备号
	//得到设备信息
	$device_info = $device_obj->device_info_by_no($no);
	if(!$device_info){
		$rtn['code'] = 1104;    //设备不存在
		api_echo_json($rtn);
	}
	$bind_list = $device_obj->bind_list($device_info['id'], array('state'=>1));
	if(!$bind_list){
		$rtn['code'] = 1105;    //设备没有指定项目
		api_echo_json($rtn);
	}
	if(count($bind_list) > 1){
		$rtn['code'] = 1106;    //设备绑定了多个项目
		api_echo_json($rtn);
	}
	$bind_info = $bind_list[0];
	//得到绑定的项目信息
	$project_info = $project_obj->info($bind_info['pid']);
	if($project_info['state'] == 2){
		$rtn['code'] = 1201;    //项目已关闭
		api_echo_json($rtn);
	}

	$type=array(6,7,8);
	$result=$device_obj->setinfo_get($project_info['uid'],$type);
	if(empty($result)){
		$rtn['code'] = 1202;
		api_echo_json($rtn);
	}

	foreach($result as $val){
		$_ary[$val['type']][]=$val['code'];
	}

	if(empty($_ary[6])){
		$_ary[6]=false;	 //油罐编号未设置
	}

	if(empty($_ary[7])){
		$_ary[7]=false; //设备编号未设置
	}

	if(empty($_ary[8])){
		$_ary[8]=false; //设备编号未设置
	}

	$res['yg']=$_ary[6];
	$res['sb']=$_ary[7];
	$res['gly']=$_ary[8];
	$news=array(
		array('color'=>"#FF0000",'text'=>"【顺运通】一款土石方工程智慧管理神器"),
		array('color'=>"#000000",'text'=>"服务热线：13996100033 13368136787"),
		array('color'=>"#006633",'text'=>"地址：中国重庆渝北区新南路145号"),
	);
	$res['news']=$news;
	api_echo_json($res);
}



/**
 * 油料登记记录
 * @param int $type  1-加油  2-进油
 */
function gas_act_record(){
	global $device_obj, $project_obj;

	// $type  1-加油  2-进油
	$type=request('type',false,true,false,1);

	$rtn = array('state'=>false);
	//错误输入
	$no=request('no');
	if(empty($no)){
		$rtn = array('state'=>false, 'list'=>true);
		$_log['time']=date("Y-m-d H:i:s");
		$_log['msg']=$_REQUEST;
		$device_obj->recordlog(var_export($_log,true),'_err.txt');
		$rtn['code'] = 2000;
		$rtn['err'] = '设备号不存在';
		api_echo_json($rtn);
	}

	//设备信息验证
	$no = request('no');
	$no=str_replace("-","",$no);
	//得到设备信息
	$device_info = $device_obj->device_info_by_no($no);
	if(!$device_info){
		$rtn['code'] = 2001;    //设备不存在
		$rtn['err']='设备不存在';
		api_echo_json($rtn);
	}

	$bind_list = $device_obj->bind_list($device_info['id'], array('state'=>1));
	if(!$bind_list){
		$rtn['code'] = 2002;
		$rtn['err']='设备没有指定项目';
		api_echo_json($rtn);
	}
	if(count($bind_list) > 1){
		$rtn['code'] = 2003;
		$rtn['err'] = '设备绑定了多个项目';
		api_echo_json($rtn);
	}

	$bind_info = $bind_list[0];
	//得到绑定的项目信息
	$project_info = $project_obj->info($bind_info['pid']);
	if($project_info['state'] == 2){
		$rtn['code'] = 2004;    //项目已关闭
		$rtn['err'] = '项目已关闭';
		api_echo_json($rtn);
	}

	//项目管理员账号id
	$xid=$project_info['uid'];
	$admin_obj=new admin();
	$times=$admin_obj->getTime($xid);
	if(!$times || $times<=0){
		$rtn['code'] = 2005;
		$rtn['err'] = '上传次数已用完，请续费';
		api_echo_json($rtn);
	}


	//加油车辆编号（加油时不选）
	$gaser=request('gaser');
	if(empty($gaser)){
		$rtn['code'] = 2012;
		$rtn['err']='缺少油料管理员编号';
		api_echo_json($rtn);
	}

	$obj_gas=new gas();
	$gaser_info=$obj_gas->getCarinfo($gaser,$xid,8);
	if(empty($gaser_info)){
		$rtn['code'] = 2012;
		$rtn['err']='油料管理员不存在';
		api_echo_json($rtn);
	}


	//加油前图片
	if(!$_FILES['img1']){
		$rtn['code'] = 2006;
		$rtn['err']='请上传加油前图片';
		api_echo_json($rtn);
	}

	//加油后图片
	if(!$_FILES['img2']){
		$rtn['code'] = 2006;
		$rtn['err']='请上传加油前图片';
		api_echo_json($rtn);
	}

	//油价
	$price=request('price',false,true,false,0);
	/**
	if(empty($price) || !is_numeric($price) || $price<0.01 || $price>100){
		$rtn['code'] = 2007;
		$rtn['err']='请正确输入油价';
		api_echo_json($rtn);
	}
	**/

	//加油前后油表数字
	$num_start = request('num_start');  //加油前
	if(!is_numeric($num_start) || $num_start<0){
		$rtn['code'] = 2007;
		$rtn['err']='请输入加油前油表数值';
		api_echo_json($rtn);
	}

	$num_end = request('num_end');  //加油后
	if(empty($num_end)){
		$rtn['code'] = 2007;
		$rtn['err']='请输入加油后油表数值';
		api_echo_json($rtn);
	}

	//加油前，机油后时间
	$time_start = request("time_start");  //设备取点时间  20180502153345
	if(empty($time_start)){
		$rtn['code'] = 2008;
		$rtn['err']='缺少加油前时间';
		api_echo_json($rtn);
	}

	$time_end = request("time_end");  //设备取点时间  20180502153345
	if(empty($time_end)){
		$rtn['code'] = 2008;
		$rtn['err']='缺少加油后时间';
		api_echo_json($rtn);
	}

	//当前时间
	$nowtime=date('YmdHis',NOW);

	//加油前照片名称
	$picname_start = $nowtime.'_'.$device_info['id'].'_'.$type.'_'.$time_start.'_1' ;
	//加油后照片名称
    $picname_end = $nowtime.'_'.$device_info['id'].'_'.$type.'_'.$time_end.'_2' ;

	//上传加油前的图片
	$image_start = $device_obj->save_img('img1','gas/'.$project_info['id'],$picname_start);
	if(!$image_start){
		$rtn['code'] = 2009;
		$rtn['err']='加油前照片上传失败';
		api_echo_json($rtn);
	}
	$img_start = strstr($image_start['folder'], '/upload') . $image_start['name'];

	//上传加油后的照片
	$image_end = $device_obj->save_img('img2','gas/'.$project_info['id'],$picname_end);
	if(!$image_end){
		$rtn['code'] = 2009;
		$rtn['err']='加油后照片上传失败';
		api_echo_json($rtn);
	}
	$img_end = strstr($image_end['folder'], '/upload') . $image_end['name'];

	//第3张，第4张
    $img_three=$device_obj->save_img('img3','gas/'.$project_info['id'],$picname_end.'_3');
    if($_FILES['img3'])$data['img_three'] = strstr($img_three['folder'], '/upload') . $img_three['name'];
    $img_four=$device_obj->save_img('img4','gas/'.$project_info['id'],$picname_end.'_4');
    if($_FILES['img4'])$data['img_four'] = strstr($img_four['folder'], '/upload') . $img_four['name'];

	//油罐
	$gascan=request('gascan');
	if(empty($gascan)){
		$rtn['code'] = 2010;
		$rtn['err']='请选择油罐编号';
		api_echo_json($rtn);
	}

	//加油车辆编号（加油时不选）
	$carno=request('carno');
	if(1==$type && empty($carno)){
		$rtn['code'] = 2011;
		$rtn['err']='请选择加油车编号';
		api_echo_json($rtn);
	}


	//加油前后坐标
	$coord_start = request('coord_start');  //加油前
	$coord_end = request('coord_end');  //加油后

    //组合上传数据
    $data['xid']=$xid;
    $data['pid']=$project_info['id'];
    $data['did']=$device_info['id'];
    $data['type']=$type;
    $data['no']=$device_info['no'];
    $data['gaser']=$gaser;
    $data['gasername']=$gaser_info['name'];
    $data['gaserphone']=$gaser_info['phone'];
    $data['img_start']=$img_start;
    $data['img_end']=$img_end;
    $data['time_start']=strtotime($time_start);
    $data['ctime']=$data['time_end']=strtotime($time_end);
    $data['time_all']=$data['time_end']-$data['time_start'];
    $data['num_start']=$num_start;
    $data['num_end']=$num_end;
    $_num=gasnumparse($num_start,$num_end);
    $data['num_all']=$_num;
    $data['coord_start']=$coord_start;
    $data['coord_end']=$coord_end;
    $data['gascan']=$gascan;
    $data['carno']=$carno;
    $data['time']=NOW;
    $data['price']=$price;
    $data['sum']=$price*$_num;


    //检查是否重复
    if($obj_gas->isRepeat($no,$data['citme'])){
    	$rtn['code'] = 2012;    //登记失败
    	$rtn['err']='上传图片已添加';
    	api_echo_json($rtn);
    }


    //车辆编号
    if(1==$type){
        $carinfo=$obj_gas->getCarinfo($carno,$xid);
        if($carinfo){
        	$data['downer']=$carinfo['name'];
        	$data['downerphone']=$carinfo['phone'];
        	$data['cartype']=$carinfo['cartype'];
        }
    }


    $r=$obj_gas->add_gas($data);
    if(!$r){
    	$rtn['code'] = 2012;    //登记失败
    	$rtn['err']='记录添加失败';
    	api_echo_json($rtn);
    }

    //发是短信通知邮件
    $is_msg=false;
    if(1==$type && $is_msg && is_mobile($data['downerphone'])){
    	//调用短信接口发送验证码
    	require '/source/lib/Smsali.php';

    	$mobile=$data['downerphone'];
    	$type=1;
    	$content='8559';
    	$entinfo_object = new Smsali();
    	$result = $entinfo_object->sendgxmsg($mobile,$type,$content);
    }


    $rtn = array('state'=>true,'list'=>true);    //登记成功
	api_echo_json($rtn);
}


//修改加油车记录信息
function gas_act_editinfo(){
	$obj_gas=new gas();

	$user_info = $obj_gas->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	$id=request('id',false,true);
	if(!is_numeric($id) || !$id)return false;

	$info=$obj_gas->getGas($id);
	if(empty($info))return false;

	$xid=$info['xid'];
	if($user_info['group']!=6 && $user_info['id']!=$xid){
		$rtn['msg']='非法操作';
		exit(json_encode($rtn));
	}

	$data['num_start']=$num_start=request('num_start',false,true);
	if(!is_numeric($num_start) || $num_start<0){
		$rtn['msg']='加油前记录不正确';
		exit(json_encode($rtn));
	}

	$data['num_end']=$num_end=request('num_end',false,true);
	if(!is_numeric($num_end) || $num_end<=0){
		$rtn['msg']='加油后记录不正确';
		exit(json_encode($rtn));
	}

	$_num=gasnumparse($num_start, $num_end);
	$data['num_all']=$_num;

	$data['gaser']=$gaser=request('gaser');
	if(empty($gaser)){
		$rtn['msg']='请输入加油员编号';
		exit(json_encode($rtn));
	}

	$data['gascan']=$gascan=request('gascan');
	if(empty($gascan)){
		$rtn['msg']='请输入油罐编号';
		exit(json_encode($rtn));
	}

	//加油时
	if(1==$info['type']){
		$data['carno']=$carno=request('carno');
		if(empty($carno)){
			$rtn['msg']='请输入加油车编号';
			exit(json_encode($rtn));
		}
	}

	//油价
	$price=request('price',false,true,false,0);
	if($price>0){
		if($price<0.01 || $price>100){
			$rtn['msg']='请正确输入油价信息';
			exit(json_encode($rtn));
		}
		$data['price']=$price;
		$data['sum']=$price*abs($_num);

	}else{
		$data['price']=0;
		$data['sum']=0;
	}


	$cartype=request('cartype');
	$data['cartype']=trim($cartype);

	//重新编号

	$res=$obj_gas->editGas($id, $data);
	$rtn = array('state'=>false);
	if($res){
		$rtn['state'] = true;
		$rtn['info'] = $data;
		exit(echo_json($rtn));
	}

	$rtn['msg']='修改失败';
	exit(json_encode($rtn));
}


/**
 * 设置记录无效
 * @author leiqianyong 2015-10-12 14:49 PM
 */
function gas_act_nouse(){
	$obj_gas=new gas();

	$user_info = $obj_gas->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	$id=request('id',false,true);
	if(!is_numeric($id) || !$id)return false;

	$info=$obj_gas->getGas($id);
	if(empty($info))return false;

	$xid=$info['xid'];
	if($user_info['group']!=6 && $user_info['id']!=$xid){
		$rtn['msg']='非法操作';
		exit(json_encode($rtn));
	}

	$data['state']=0;
	$res = $obj_gas->editGas($id, $data);
	if($res){
		$rtn['state'] = true;
		$rtn['msg'] = '处理成功';
		exit(json_encode($rtn));
	}

	$rtn['msg'] = '处理失败';
	exit(json_encode($rtn));
}


/**
 * 设置记录有效
 * @author leiqianyong 2015-10-12 14:49 PM
 */
function gas_act_reback(){
	$obj_gas=new gas();

	$user_info = $obj_gas->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	$id=request('id',false,true);
	if(!is_numeric($id) || !$id)return false;

	$info=$obj_gas->getGas($id);
	if(empty($info))return false;

	$xid=$info['xid'];
	if($user_info['group']!=6 && $user_info['id']!=$xid){
		$rtn['msg']='非法操作';
		exit(json_encode($rtn));
	}

	$data['state']=1;
	$res = $obj_gas->editGas($id, $data);
	if($res){
		$rtn['state'] = true;
		$rtn['msg'] = '处理成功';
		exit(json_encode($rtn));
	}

	$rtn['msg'] = '处理失败';
	exit(json_encode($rtn));
}


/**
 * 彻底删除记录
 * @author leiqianyong 2015-10-12 14:49 PM
 */
function gas_act_delop(){
	$obj_gas=new gas();

	$user_info = $obj_gas->out_cookie();
	if(empty($user_info)){
		$rtn['msg']='请登录';
		exit(json_encode($rtn));
	}

	$id=request('id',false,true);
	if(!is_numeric($id) || !$id)return false;

	$info=$obj_gas->getGas($id);
	if(empty($info))return false;

	$xid=$info['xid'];
	if($user_info['group']!=6 && $user_info['id']!=$xid){
		$rtn['msg']='非法操作';
		exit(json_encode($rtn));
	}

	global $_site;
	unlink($_site->_root().$info['img_start']);
	unlink($_site->_root().$info['img_end']);
	$sql="DELETE FROM syt_gas WHERE id={$id}";
	$obj_gas->_mysql(0)->query($sql);

	$rtn['msg'] = '处理成功';
	exit(json_encode($rtn));
}



//进油明细
function gas_act_improt_in(){
    gas_act_improt_out(2);
}


//导出油料明细
function gas_act_improt_out($type=1){
	$gas_obj=new gas();
	$data = $_GET;

	$data['state']=1;
	$data['report']=1;
	$list=$gas_obj->get_list($data);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}


	$title_ext = (1==$type)?'加油明细表':'进油明细表' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

	if(1==$type){
	   $title = array('油罐号','加油员工号','加油设备号','车辆型号','年月','日期','加油开始时间','加油结束时间','加油时长（秒）','加油前数字','加油后数字','加油量','油价','金额');
	}else{
	   $title = array('油罐号','加油员工号','年月','日期','进油开始时间','进油结束时间','进油时长（秒）','进油前数字','进油后数字','进油量','油价','金额');
	}
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_num = 0 ;
	if (!empty($list)){
		foreach($list as $key=>$cv){

			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=center x:str>'.($cv['gascan']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['gaser']).'</strong></td>' ;
			if(1==$type){
			   $html .= '<td width=”100″ align=left x:str>'.$cv['carno'].'</td>' ;
			   $html .= '<td width=”100″ align=left x:str>'.$cv['cartype'].'</td>' ;
			}
			$html .= '<td width=”100″ align=left x:str>'.date('Y-m',$cv['ctime']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.(int)date('d',$cv['ctime']).'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.date('H:i:s',$cv['time_start']).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.date('H:i:s',$cv['time_end']).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($cv['time_end']-$cv['time_start']).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($cv['num_start']).'</td>' ;
			$html .= '<td width=”100″ align=right x:num>'.($cv['num_end']).'</td>' ;
			$_num = gasnumparse($cv['num_start'],$cv['num_end']);
			$html .= '<td width=”100″ align=right x:num>'.($_num).'</td>' ;
			$_price=$cv['price']>0?sprintf("%.2f",$cv['price']):'';
			$html .= '<td width=”100″ align=right x:str>'.($_price).'</td>' ;
			$_sum=$cv['sum']>0?sprintf("%.2f",$cv['sum']):'';
			$html .= '<td width=”100″ align=right x:str>'.($_sum).'</td>' ;
			$html .= '</tr>' ;
			$_num++;
		}
	}

	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;
}

//导出统计报表
function gas_act_improt_report(){
	$gas_obj=new gas();
	$data = $_GET;

	$list=$gas_obj->get_report($data);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}


	$title_ext = '加油数量统计' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;

	$title = array('加油车编号','产权人','加油次数','加油量','金额');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_num = 0 ;
	if (!empty($list)){
		$num=0;
		$all=0;
		foreach($list as $key=>$cv){
			$num+=$cv['number'];
			$all+=$cv['gasall'];
			$sum+=$cv['gassum'];

			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['carno'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['downer'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['number'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['gasall'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.($cv['gassum']>0?$cv['gassum']:'').'</td>' ;
			$html .= '</tr>' ;
			$_num++;
		}

		$html .= '<tr><td align=left x:str>合计</td><td></td><td align=right x:str>'.$num.'</td><td align=right x:str>'.$all.'</td><td align=right x:str>'.$sum.'</td></tr>';
	}

	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;

}


//进油统计时间
function gas_act_improt_in_report(){
	$gas_obj=new gas();
	$data = $_GET;

	$list=$gas_obj->get_in_report($data);
	if(empty($list)){
		echo 'No Data !' ;
		exit;
	}


	$title_ext = '进油数量统计' ;
	$filename = "SYT_REPORT:".date('YmdHis') ;

	$title = array('油罐编号','进油次数','加油量','金额');
	$cols_num = count($title);
	$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

	$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

	//开始时间
	if($data['start_time'] || $data['end_time']){
		$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
	}

	//导出xls 开始
	if (!empty($title)){
		$html .= '<tr>' ;
		foreach ($title as $k => $v) {
			$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
		}
		$html .= '</tr>' ;
	}

	$_num = 0 ;
	if (!empty($list)){
		$num=0;
		$all=0;
		$sum=0;
		foreach($list as $key=>$cv){
	        $num+=$cv['number'];
	        $all+=$cv['gasall'];
	        $sum+=$cv['gassum'];

			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['gascan'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['number'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['gasall'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.$cv['gassum'].'</td>' ;
			$html .= '</tr>' ;
			$_num++;
		}

	    $html .= '<tr><td align=left x:str>合计</td><td align=right x:str>'.$num.'</td><td align=right x:str>'.$all.'</td><td align=right x:str>'.$sum.'</td></tr>';
	}

	$html .= '</body></html></table>';

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename={$filename}.xls");
	echo $html ;

}



//function
//*+------------------------------------------------------------------------+*/

//加油总量转换
function gasnumparse($start,$end){
	$sum=$end-$start;
	return $sum>=0?$sum:(10000+$sum);
}

